package com.loan.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.loan.entity.Legal;
import com.loan.entity.Result;
import com.loan.mapper.LegalMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author
 * @since 2024-09-28
 */
@RestController
@RequestMapping("/legal")
public class LegalController {

    @Resource
    RedisTemplate redisTemplate;

    @Autowired
    LegalMapper legalMapper;

    @RequestMapping(value = "/getLegalList",method = RequestMethod.POST)
    public List<Legal> getLegalList(){
        List<Legal> legalList = legalMapper.selectList(null);
        return legalList;
    }
    @GetMapping("/getlegalById")
    public Legal getlegalById(){
        Object uids = redisTemplate.opsForValue().get("uid");
        Integer uid = (Integer) uids;
        Map<String,Object> columnMap=new HashMap<>();
        columnMap.put("uid",uid);
        List<Legal> legalList = legalMapper.selectByMap(columnMap);
        Legal legal = new Legal();
        legal=legalList.get(0);
        return legal;

    }

    @PostMapping("/updatelegalById")
    public Result updatelegalById(String borrowInfoStatus){

        Object uids = redisTemplate.opsForValue().get("uid");
        Integer uid = (Integer) uids;

        //更新
        Legal legal = new Legal();
        legal.setBorrowInfoStatus(borrowInfoStatus);
        QueryWrapper<Legal> wrapper=new QueryWrapper<Legal>();
        wrapper.eq("uid",uid);
        int count=legalMapper.update(legal,wrapper);

        Result result = new Result();
        if (count>0){
            result.setSuccess(true);
            return result;
        }
        result.setSuccess(false);
        return result;

    }

    @PostMapping("/addLegal")
    public Result addLegal(Legal legal){

        Date date = new Date();
        String str = "yyy-MM-dd HH:mm:ss";
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat(str);
        String df = simpleDateFormat.format(date);

        Object uids = redisTemplate.opsForValue().get("uid");
        Integer uid = (Integer) uids;

        Legal legal1 = new Legal();
        legal1.setUid(uid);
        legal1.setDateTime(new Date());
        legal1.setBank(legal.getBank());
        legal1.setYeard(legal.getYeard());
        //借款金额
        int TotalMoney= Integer.parseInt(legal.getTotalMoney())*10;
        legal1.setTotalMoney(String.valueOf(TotalMoney));
        //贷款年限
        int yeard= Integer.parseInt(legal.getYeard());
        //bankRate借款利率
        float bankRate= Float.parseFloat(legal.getBankRate());
        //利息interestMoney
        float interestMoney=TotalMoney*bankRate;
        legal1.setInterestMoney(String.valueOf(interestMoney));
        //sumMoney总还款金额
        float sumMoney = TotalMoney + interestMoney;
        legal1.setSumMoney(String.valueOf(sumMoney));
        //已还款金额payMoney
        legal1.setPayMoney("0");
        //月还款金额payMonthMoney
        float payMonthMoney = sumMoney/yeard;
        legal1.setPayMonthMoney(String.valueOf(payMonthMoney));
        //剩余还款金额lastMoney
        float lastMoney = sumMoney;
        legal1.setLastMoney(String.valueOf(lastMoney));

        legal1.setTotalMoney(String.valueOf(TotalMoney));
        legal1.setBankAccount(legal.getBankAccount());
        legal1.setTextArea(legal.getTextArea());
        legal1.setLegalName(legal.getLegalName());
        legal1.setLegalAge(legal.getLegalAge());
        legal1.setLegalSex(legal.getLegalSex());
        legal1.setBorrowInfoStatus("1");
        legal1.setContactsRelation(legal.getContactsRelation());
        legal1.setBankRate(legal.getBankRate());
        legal1.setIdCard1(legal.getIdCard1());
        legal1.setIdCard3(legal.getIdCard3());
        legal1.setIdCard2(legal.getIdCard2());

        int count =  legalMapper.insert(legal1);
        Result result = new Result();
        if (count>0){
            result.setSuccess(true);
            return result;
        }
        result.setSuccess(false);
        return result;
    }


}

